<?php
/**
 * Created by PhpStorm.
 * User: chenxiang
 * Date: 2017/4/30
 * Time: 10:59
 */

namespace App\Http\Controllers\Home;


use App\Model\Json;
use App\Model\Permission;
use App\Model\Role;
use App\Model\RolePermission;
use Illuminate\Http\Request;

class RoleController extends CommonController
{
    public function index(){
        $role = Role::paginate(15);
        return view('Home.Role.index',compact('role'));
    }

    public function add(Request $request){
        $data = $request->only('name');
        return Role::role_create($data);
    }


    public function delete(Request $request){
        $id = $request->input('id');
        if(Role::where('id',$id)->delete()){
            return Json::success('删除成功','删除成功');
        }else{
            return Json::error('删除失败','删除失败');
        }
    }

    // 获取 角色 拥有权限 和 剩余权限
    public function permission(Request $request){
        $id = $request->input('id');
        if(!$id){
            return Json::error('id为空');
        }
        $RolePermission = RolePermission::where('role',$id)->get();
        foreach($RolePermission as $val){
            $ids[] = $val->permission;
        }
//        $model = new Permission();
//        $model->fillable[] = "(id in (".implode(',',$ids)."))";
//        dd($model);
        if(isset($ids)){
            $RolePermission = Permission::orderBy('action')->whereIn('id',$ids)->get();
            $Permission = Permission::orderBy('action')->whereNotIn('id',$ids)->get();
        }else{
            $RolePermission = array();
            $Permission = Permission::orderBy('action')->get();
        }
        return Json::success(compact('Permission','RolePermission'),'获取成功');
    }

    // 设置 角色 拥有权限 和 剩余权限
    public function setPermission(Request $request){
//        $time = date('Y-m-d H:i:s');
        $data = $request->only('id','Permission','RolePermission');
        if($data['Permission']){
            $Permission = Permission::whereIn('id',$data['Permission'])->get();
            if($Permission->toArray()){
                foreach($Permission as $val){
                    $r_p = array('role'=>$data['id'],'permission'=>$val->id,'admin_id'=>auth()->user()->id);
                    $r_p = RolePermission::RPcreate($r_p);
                    if(!$r_p['status']){
                        $data['Permission'] = array_diff($data['Permission'],array($val->id));
                    }
                }
            }
        }
        if($data['RolePermission']){
            $RolePermission = RolePermission::where('role',$data['id'])->whereIn('permission',$data['RolePermission'])->get();
            foreach($RolePermission as $val){
                $val->admin_id = auth()->user()->id;
//                $val->deleted_at = $time;
                if($val->save() && $val->delete()){
                    $date['RolePermission'] = array_diff($data['RolePermission'],array($val->permission));
                }
            }
        }
        $res = array('RolePermission'=>$data['RolePermission'],'Permission'=>$data['Permission']);
        return Json::success($res);
    }


}